﻿package mortal.game.view.firstReCharge
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import __AS3__.vec.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.player.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.rebate.data.*;
    import mortal.mvc.core.*;

    public class FirstRechargeDailyModule extends Window
    {
        private var _bg:GImageBitmap;
        private var _todayRewardBitmap:GImageBitmap;
        private var _reChargeBtn:GLabelButton;
        private var _getRewardBtn:GLoadingButton;
        private var _time:int;
        private var _img2d:Img2D;
        private var _rect3d:Rect3DObject;
        private var _bodyPlayer:WizardActionPlayer;
        private var timerContorler:TimeControler;
        private var _rewardArr:Array;
        private var _rewardList:Vector.<EffectItem>;
        private var _serverOpenDay:int;
        private static var _instance:FirstRechargeDailyModule;

        public function FirstRechargeDailyModule()
        {
            this._rewardArr = [GameConst.TDay1FirstRechargeGiftCode, GameConst.TDay2FirstRechargeGiftCode, GameConst.TDay3FirstRechargeGiftCode, GameConst.TDay4FirstRechargeGiftCode, GameConst.TDay5FirstRechargeGiftCode, GameConst.TDay6FirstRechargeGiftCode, GameConst.TDay7FirstRechargeGiftCode];
            this._rewardList = new Vector.<EffectItem>;
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.timerContorler = TimeControler.createTimerInstance();
            this.timerContorler.unActive();
            this.setSize(720, 400);
            titleHeight = 80;
            this._bg = UIFactory.gImageBitmap("", 0, 0);
            this._todayRewardBitmap = UIFactory.gImageBitmap("After7DayFirstRecharge.png", 70, 75, _titleSprite);
            this._reChargeBtn = UIFactory.gLabelButton("", GLabelButton.gLoadingButton, ImagesConst.VipReCharge_upSkin, 230, 325, 270, 40, _contentTopOf3DSprite);
            this._reChargeBtn.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            this._getRewardBtn = UIFactory.gLoadingButton(ResFileConst.GetGiftBtn2, 293, 343, 112, 32, _contentTopOf3DSprite);
            this._getRewardBtn.configEventListener(MouseEvent.CLICK, this.getRewardHandler);
            this._rect3d = Rect3DManager.instance.getRect3dByWindow(this);
            if (!this._rect3d)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(0, 0, 716, 394), this);
            }
            LoaderHelp.setBitmapdata("FirstRechagerBg.swf", this._bg, 0, 0, -1, -1, this.add2d);
            LoaderHelp.addResCallBack(ResFileConst.FirstReChargeModule, this.showSkin);
            Dispatcher.addEventListener(EventName.WindowShowed, this.onWindowShow);
            return;
        }// end function

        protected function onWindowShow(event:DataEvent) : void
        {
            if (event.data != this)
            {
                this.hide();
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            Dispatcher.removeEventListener(EventName.WindowShowed, this.onWindowShow);
            this._bg.dispose(param1);
            this._bg = null;
            this._reChargeBtn.dispose(param1);
            this._reChargeBtn = null;
            this._getRewardBtn.dispose(param1);
            this._getRewardBtn = null;
            this._todayRewardBitmap.dispose(param1);
            this._todayRewardBitmap = null;
            if (this._img2d)
            {
                this._img2d.dispose(param1);
                this._img2d = null;
            }
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            if (this._bodyPlayer)
            {
                this.timerContorler.unActive();
                FrEventDispatcher.instance.proxyRemoveEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._bodyPlayer.dispose(true);
                this._bodyPlayer = null;
            }
            this._rewardList.length = 0;
            this._serverOpenDay = 0;
            super.disposeImpl(param1);
            return;
        }// end function

        public function set serverOpenDay(param1:int) : void
        {
            this._serverOpenDay = param1;
            this.updateReward();
            this.updateFirstRechargeInfo(Cache.instance.firstRecharge.firstRechargeRewardInfo);
            return;
        }// end function

        private function showSkin() : void
        {
            if (isDisposed)
            {
                return;
            }
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.FirstRechargeTitle, 160, -45, _contentTopOf3DSprite));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.FirstRechargeTxt1, 70, 25, _contentTopOf3DSprite));
            var _loc_1:* = UIFactory.gBitmap(ImagesConst.FirstRechargeRewardBg, 126, 210, null);
            _contentTopOf3DSprite.addChildAt(_loc_1, 0);
            this.pushUIToDisposeVec(_loc_1);
            this._reChargeBtn.label = ImagesConst.FirstRechargeBtnLabel;
            return;
        }// end function

        private function add3d() : void
        {
            var _loc_1:* = ModelConfig.instance.getInfoByCode(int(WizardConfig.instance.getSpriteByCodeAndLevel(2, 1).modelId));
            var _loc_2:* = _loc_1.mesh1 + ".md5mesh";
            var _loc_3:* = _loc_1.bone1 + ".skeleton";
            if (this._bodyPlayer == null)
            {
                this._bodyPlayer = FrObjectPool.getObject(WizardActionPlayer, this);
                FrEventDispatcher.instance.proxyAddEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._bodyPlayer.timerContorler = this.timerContorler;
            }
            this.timerContorler.active();
            this._bodyPlayer.clearExtend();
            this._bodyPlayer.load(_loc_2, _loc_3, _loc_1.texture1, this._rect3d.renderList);
            if (_loc_1.mesh2)
            {
                _loc_2 = _loc_1.mesh2 + ".md5mesh";
                _loc_3 = _loc_1.bone2 + ".skeleton";
                this._bodyPlayer.loadExtend(_loc_2, _loc_3, _loc_1.texture2, this._rect3d.renderList);
                this._bodyPlayer.extendPlayer.timerContorler = this.timerContorler;
            }
            this._bodyPlayer.play();
            this._bodyPlayer.scaleValue = 3;
            this._bodyPlayer.rotationY = 0;
            this._rect3d.addObject3d(this._bodyPlayer, 0, 370, false);
            this._bodyPlayer.changeAction(ActionName.Standing);
            return;
        }// end function

        private function add2d() : void
        {
            this._img2d = new Img2D(null, this._bg.bitmapData, new Rectangle(0, 0, 736, 400), true);
            this._rect3d.addImg(this._img2d);
            this.add3d();
            return;
        }// end function

        protected function onInitBodyPlayer(event:Event) : void
        {
            if (this._bodyPlayer)
            {
                this._bodyPlayer.addFrameComplete(ActionName.Standing, this.onFrameComplete);
            }
            return;
        }// end function

        private function onFrameComplete() : void
        {
            if (!this._bodyPlayer)
            {
                return;
            }
            this.setActionName(ActionName.Stand);
            return;
        }// end function

        public function setActionName(param1:String) : void
        {
            this._bodyPlayer.changeAction(param1);
            return;
        }// end function

        private function updateReward() : void
        {
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:ItemData = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:EffectItem = null;
            var _loc_1:* = RebateUtil.getEveryDayFirstGiftCode();
            var _loc_2:* = ItemConfig.instance.getConfig(_loc_1) as ItemGiftBagInfo;
            if (_loc_2)
            {
                _loc_3 = _loc_2.itemList.split("&");
                _loc_6 = _loc_3.length;
                while (_loc_7 < _loc_6)
                {
                    
                    if (_loc_3[_loc_7])
                    {
                        _loc_4 = _loc_3[_loc_7].split("#");
                        _loc_5 = new ItemData(int(_loc_4[1]));
                        if (_loc_5.itemInfo && ItemsUtil.isFitCarrer(_loc_5))
                        {
                            _loc_10 = UICompomentPool.getUICompoment(EffectItem);
                            _loc_10.setItemStyle(ItemStyleConst.Big, ImagesConst.RewardItemBigBg, 4, 4);
                            _loc_10.isShowLock = true;
                            _loc_10.itemData = _loc_5;
                            _loc_10.amount = _loc_4[2];
                            _contentTopOf3DSprite.addChild(_loc_10);
                            this.pushUIToDisposeVec(_loc_10);
                            this._rewardList.push(_loc_10);
                        }
                    }
                    _loc_7++;
                }
                _loc_6 = this._rewardList.length;
                _loc_8 = 80;
                _loc_9 = (this.width - _loc_6 * 80) * 0.5 + 17;
                _loc_7 = 0;
                while (_loc_7 < _loc_6)
                {
                    
                    this._rewardList[_loc_7].x = _loc_9 + _loc_8 * _loc_7;
                    this._rewardList[_loc_7].y = 245;
                    this._rewardList[_loc_7].showEffect(true, 36, 32, "ArriundEffect_cheng_big.swf");
                    _loc_7++;
                }
            }
            return;
        }// end function

        private function onMouseClick(event:MouseEvent) : void
        {
            GameUtil.gotoRecharge(ModuleTypeConst.FirstRecharge);
            return;
        }// end function

        private function getRewardHandler(event:MouseEvent) : void
        {
            GameProxy.role.getDayFirstReward();
            return;
        }// end function

        public function updateFirstRechargeInfo(param1:SDayFirstRechargeRewardInfo) : void
        {
            if (param1.rechargeNum > 0 && !param1.isGet)
            {
                this._getRewardBtn.visible = true;
                this._reChargeBtn.visible = false;
            }
            else
            {
                this._getRewardBtn.visible = false;
                this._reChargeBtn.visible = true;
            }
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            LayerManager.windowLayer.removeAllWindow();
            LayerManager.windowLayer3D.removeAllWindow();
            super.show();
            this.x = Global.stage.stageWidth - this.width >> 1;
            this.y = Global.stage.stageHeight - this.height >> 1;
            return;
        }// end function

        public static function get instance() : FirstRechargeDailyModule
        {
            if (_instance == null)
            {
                _instance = new FirstRechargeDailyModule;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
